﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using Newtonsoft.Json;
using ScannerWatcher.Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net.Cache;
using System.Text;

namespace ScannerWatcher.Service
{
    public static class CaseHelper
    {

        public static List<case_question_category> QuestionCategoryTree { get; set; }
        public static List<caseinfo> CaseList { get; set; }

        static CaseHelper()
        {
            QuestionCategoryTree = new List<case_question_category>();
            CaseList = new List<caseinfo>();
        }

        public static void LoadCaseInfo(string userId)
        {
            var response = HttpHelper.Get(Settings.API_GetOngoingCases + "?userId=" + userId);

            var result = JsonConvert.DeserializeObject<HResult<List<caseinfo>>>(response);

            if (result.success)
            {
                CaseList.AddRange(result.data);
            }
        }

        public static HResult<List<case_question_category>> GetCaseQuestionCategory(string caseId, string parentId)
        {
            var response = HttpHelper.Get(Settings.API_GetCaseTreeChildNodes + "?caseId=" + caseId + "&parentId=" + parentId);

            var result = JsonConvert.DeserializeObject<HResult<List<case_question_category>>>(response);

            return result;
        }

        public static HResult<case_question_category> GetUndefinedCategory(string caseId)
        {
            var response = HttpHelper.Get(Settings.API_GetCaseUndefinedCategory + "?caseId=" + caseId);

            var result = JsonConvert.DeserializeObject<HResult<case_question_category>>(response);

            return result;
        }

        public static HResult<FileDto> UploadFile(string caseId, string filePath)
        {
            var data = new NameValueCollection();
            data.Add("caseId", caseId);

            var response = HttpHelper.HttpUploadFile(Settings.API_UploadAttachFile, filePath, data);

            var result = JsonConvert.DeserializeObject<HResult<FileDto>>(response);

            return result;

        }

        public static HResult<case_question_fact_attach> AddCaseCategory(AddDto category)
        {
            //var data = new Dictionary<string, string>();
            //foreach (var prop in category.GetType().GetProperties())
            //{
            //    data.Add(prop.Name, prop.GetValue(category, null).ToString());
            //}

            string json = JsonConvert.SerializeObject(category);

            var response = HttpHelper.Post(Settings.API_AddFactAttach, json);

            var result = JsonConvert.DeserializeObject<HResult<case_question_fact_attach>>(response);

            return result;
        }
    }
}
